package com.sanji.sjmallconfig;

import android.content.Context;
import android.os.Environment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.module.GlideModule;
import com.sanji.sjmallutils.SDCardUtil;

import java.io.File;

/**
 * Created by fhm on 2016/12/12.
 */

public class MyGlideConfig implements GlideModule {
    //设置
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        //设置缓存、线程池的大小
        MemorySizeCalculator calculator = new MemorySizeCalculator(context);
        int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
        int defaultBitmapPoolSize = calculator.getBitmapPoolSize();

        int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
        int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);

        builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
        builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
        //设置磁盘缓存大小和路径
        int cacheSize = 100 * 1024 * 1024;
        //获取sd卡的根目录
        String downloadDirectoryPath = SDCardUtil.getSdPath(context)+SDCardUtil.FILECACHE;
        builder.setDiskCache(new DiskLruCacheFactory(downloadDirectoryPath, cacheSize));
        //设置图片显示格式（默认的格式为PREFER_RGB_565）
//        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
    }

    @Override
    public void registerComponents(Context context, Glide glide) {

    }
}
